import random

Bank = {
    'name': '中国农业银行',
    'users': []
}

# 用户账户
class UserAccount:
    def __init__(self, id, accType, name, password, address, balance):
        self.id = str(id)
        self.type = accType
        self.name = name
        self.password = str(password)
        self.address = address
        self.balance = balance
        self.bankname = '中国农业银行'

    def __str__(self):
        return f'用户名：{self.name}，账户ID：{self.id}，地址：{self.address}，余额：{self.balance}'


# ATM界面
def BankView():
    print('******************************')
    print('*     中国农业银行账户管理系统    *')
    print('******************************')
    print('*            选项             *')
    print('*           1.开户            *')
    print('*           2.存钱            *')
    print('*           3.取钱            *')
    print('*           4.转账            *')
    print('*           5.查询            *')
    print('*           6.退出            *')


def FindAccountById(acc_id):
    for user in Bank['users']:
        if user.id == acc_id:
            return user
    return None


# 开户
def CreateAccount(useraccount):
    if len(Bank['users']) >= 100:
        print('对不起，该银行用户已满')
        return 3
    for u in Bank['users']:
        if u.id == useraccount.id:
            print('对不起，该用户已存在')
            return 2
    Bank['users'].append(useraccount)
    print('开户成功，账户ID为：', useraccount.id)
    return 1


# 存钱
def SaveMoney(acc_id, password, money):
    user = FindAccountById(acc_id)
    if user and user.password == str(password):
        user.balance += money
        print('存钱成功')
        return True
    print('账户或密码错误')
    return False


# 取钱（含类型限制）
def TakeMoney(acc_id, password, money):
    user = FindAccountById(acc_id)
    if not user:
        print('账户不存在')
        return 1
    if user.password != str(password):
        print('密码错误')
        return 2

    # 类型限制
    limit = 50000 if user.type == 1 else 20000
    if money > limit:
        print(f'该账户类型单次最多可取{limit}元')
        return 4
    if user.balance >= money:
        user.balance -= money
        print('取钱成功')
        return 0
    else:
        print('余额不足')
        return 3


# 转账（含类型限制）
def TransferMoney(accout_id, accin_id, password, money):
    user_out = FindAccountById(accout_id)
    user_in = FindAccountById(accin_id)

    if not user_out:
        print('转出账户不存在')
        return 1
    if user_out.password != str(password):
        print('密码错误')
        return 2
    if not user_in:
        print('转入账户不存在')
        return 1

    # 类型限制
    limit = 50000 if user_out.type == 1 else 20000
    if money > limit:
        print(f'该账户类型单次最多可转出{limit}元')
        return 4
    if user_out.balance < money:
        print('余额不足')
        return 3

    user_out.balance -= money
    user_in.balance += money
    print('转账成功')
    return 0


# 查询账户
def SearchAccount(acc_id, password):
    user = FindAccountById(acc_id)
    if user and user.password == str(password):
        print(user)
    else:
        print('账户或密码错误')


def BankSystem():
    while True:
        BankView()
        opt = input('请输入选择：')
        if opt == '1':
            accountType = int(input('请输入开户类型 1.金卡最大转账额为5万，转出最大5万，转入没限制 2.普通卡最大转账额为2万，转出最大2万，转入没限制：'))
            if accountType not in [1, 2]:
                print('输入错误，请重新输入')
                continue
            randomaccount = random.randint(100000, 999999)
            name = input('请输入用户名：')
            password = input('请输入密码(6位数字)：')
            querenpassword = input('请再次输入密码：')
            if password != querenpassword:
                print('两次密码不一致')
                continue
            guojia = input('请输入国家：')
            shengfen = input('请输入省份：')
            menpaihao = input('请输入门牌号：')
            address = guojia + shengfen + menpaihao
            newaccount = UserAccount(str(randomaccount), accountType, name, password, address, 0)
            CreateAccount(newaccount)
        elif opt == '2':
            acc = input('请输入账户：')
            pasd = input('请输入密码：')
            money = int(input('请输入金额：'))
            SaveMoney(acc, pasd, money)
        elif opt == '3':
            acc = input('请输入账户：')
            pasd = input('请输入密码：')
            money = int(input('请输入金额：'))
            TakeMoney(acc, pasd, money)
        elif opt == '4':
            accout = input('请输入转出账户：')
            pasd = input('请输入转出账户密码：')
            accoutin = input('请输入转入账户：')
            money = int(input('请输入金额：'))
            TransferMoney(accout, accoutin, pasd, money)
        elif opt == '5':
            acc = input('请输入账户：')
            pasd = input('请输入密码：')
            SearchAccount(acc, pasd)
        elif opt == '6':
            print('退出系统')
            break
        else:
            print('输入错误，请重新输入')


if __name__ == '__main__':
    BankSystem()
